from Autotests.LocalMachine.UiObjectsMap.AirClient import Options
from Autotests.LocalMachine.Controls.BaseControl import BaseWebControl
from Autotests.LocalMachine.Controls.Button import WebButton
from Autotests.LocalMachine.Controls.ListView import WebListView
from Autotests.LocalMachine.Controls.LineEdit import WebLineEdit
from Autotests.LocalMachine.Controls.CheckBox import WebDefaultCheckBox
from OptionsPage import OptionsPage


class EditScriptTab(OptionsPage):
    def __init__(self):
        super(EditScriptTab, self).__init__()
        self._textBoxScriptName = WebLineEdit(Options.Scripts.EditScript.txtboxScriptName)
        self._textBoxUserName = WebLineEdit(Options.Scripts.EditScript.txtboxUserName)
        self._textBoxPassword = WebLineEdit(Options.Scripts.EditScript.txtboxPassword)
        self._textBoxTimeout = WebLineEdit(Options.Scripts.EditScript.txtboxTimeout)
        self._checkBoxFailOnError = WebDefaultCheckBox(Options.Scripts.EditScript.checkboxFailOnError)
        self._textBoxErrorMessage = WebLineEdit(Options.Scripts.EditScript.txtboxErrorMessage)

    def save(self):
        WebButton(Options.Scripts.EditScript.btnSave).click()

    def goToHomeLocation(self):
        pass

    def cancel(self):
        WebButton(Options.Scripts.EditScript.btnCancel).click()

    @OptionsPage.setter
    def modyfyScript(self, scriptName, userName, password, timeout, errorMessage, chboxState):
        self._textBoxScriptName.typeText(scriptName)
        self._textBoxUserName.typeText(userName)
        self._textBoxPassword.typeText(password)
        self._textBoxTimeout.typeText(timeout)
        self._textBoxErrorMessage.typeText(errorMessage)
        self._checkBoxFailOnError.setCheckState(chboxState)

    @OptionsPage.getter
    def getScriptInfo(self):
        name = self._textBoxScriptName.text()
        user = self._textBoxUserName.text()
        password = self._textBoxPassword.text()
        timeout = self._textBoxTimeout.text()
        errorMessage = self._textBoxErrorMessage.text()
        isFailOnError = self._checkBoxFailOnError.isChecked()
        return name, user, password, timeout, errorMessage, isFailOnError


class ScriptsTab(OptionsPage):
    def __init__(self):
        super(ScriptsTab, self).__init__()
        self._buttonAdd = WebButton(Options.Scripts.btnAdd)
        self._buttonEdit = WebButton(Options.Scripts.btnEdit)
        self._buttonDelete = WebButton(Options.Scripts.btnDelete)
        self._listBoxScripts = WebListView(Options.Scripts.listBoxScripts)
        self._dialogEdit = BaseWebControl(Options.Scripts.winDialogEdit)
        self._editScriptTab = EditScriptTab()

    def goToHomeLocation(self):
        super(ScriptsTab, self).goToHomeLocation()
        BaseWebControl(Options.btnScripts).click()

    @OptionsPage.setter
    def addScript(self, scriptName, userName = 'test', password = 'test',
                  timeout = 10, errorMessage = "Error", chboxState = False):
        self._buttonAdd.click()
        self._editScriptTab.modyfyScript(scriptName, userName, password, timeout, errorMessage, chboxState)

    @OptionsPage.setter
    def modifyScript(self, oldScriptName, newScriptName, newUserName,
                   newPassword, newTimeout, newErrorMessage, chboxState):
        self._listBoxScripts.selectItemsByText(oldScriptName)
        self._buttonEdit.click()
        self._editScriptTab.modyfyScript(newScriptName, newUserName, newPassword,
                                         newTimeout, newErrorMessage, chboxState)

    @OptionsPage.getter
    def getScriptCount(self):
        return self._listBoxScripts.rowsCount()

    @OptionsPage.getter
    def hasScript(self, script):
        return self._listBoxScripts.hasItemsWithText(script)

    @OptionsPage.setter
    def deleteScript(self, script):
        self._listBoxScripts.selectItemsByText(script)
        self._buttonDelete.click()

    @OptionsPage.getter
    def getScriptInfo(self, scriptName):
        self._listBoxScripts.selectItemsByText(scriptName)
        self._buttonEdit.click()
        return self._editScriptTab.getScriptInfo()

    def viewEditScript(self, script=''):
        self.goToHomeLocation()
        if script == '':
            self._buttonAdd.click()
        else:
            self._listBoxScripts.selectItemsByText(script)
            self._buttonEdit.click()
